/*
 * Copyright 2012 Oracle and/or its affiliates.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developers.sun.com/license/berkeley_license.html
 */


package boundless.web;

import boundless.ejb.RequestBean;
import boundless.entities.Account;
//import com.forest.entity.Groups;
//import com.forest.entity.Person;
//import com.forest.qualifiers.LoggedIn;
//import com.forest.web.util.JsfUtil;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 *
 * @author markito
 */
@Named(value = "userController")
@SessionScoped
public class UserController implements Serializable {
    @EJB
    private RequestBean req;
    
    Account user;
    private String password;
    private String username;    
    
    /**
     * Creates a new instance of Login
     */
    public UserController() {
    }

    /**
     * Login method based on
     * <code>HttpServletRequest</code> and security realm
     */
    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext()
                                                                 .getRequest();
        String result;

        try {
            request.login(
                this.getUsername(),
                this.getPassword());

            this.user = req.getUserByUsername(username);
            result = "loginUser";
        } catch (ServletException ex) {
            Logger.getLogger(UserController.class.getName())
                  .log(Level.SEVERE, null, ex);
            //JsfUtil.addErrorMessage(
            //        JsfUtil.getStringFromBundle(BUNDLE, "Login_Failed"));

            result = "loginError";
        }

        return result;
    }

    public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext()
                                                                 .getRequest();

        try {
            this.user = null;
            request.logout();

            ((HttpSession) context.getExternalContext()
                                  .getSession(false)).invalidate();
        } catch (ServletException ex) {
            Logger.getLogger(UserController.class.getName())
                  .log(Level.SEVERE, null, ex);
        } finally {
            return "/index.xhtml?faces-redirect=true";
        }
    }

    @Produces
    @LoggedIn
    public Account getAuthenticatedUser() {
        return user;
    }

    public boolean isLogged() {
        return (getUser() == null) ? false : true;
    }

     /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the user
     */
    public Account getUser() {
        return user;
    }
}
